package algotithm.interview.baidu.test1;

import java.io.*;
import java.util.*;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/17-19:20
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(reader.readLine());
        String s = reader.readLine();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
        }
        int num = lowestJump(arr);
        writer.write(num + "\n");
        writer.flush();
        writer.close();
        reader.close();
    }

    public static int lowestJump(int[] arr) {
        int n = arr.length;
        int res = 0;
        int end = 0;
        HashMap<Integer,List<Integer> > map = new HashMap<>(64);
        int[] dp = new int[n];
        Arrays.fill(dp,88888888);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            List<Integer> list ;
            if (map.containsKey(arr[i])) {
                list = map.get(arr[i]);
            } else {
                list = new ArrayList<>();
            }
            list.add(i);
            map.put(arr[i],list);
        }
        Queue<Integer> q = new LinkedList<>();
        q.add(0);
        while (!q.isEmpty()) {
            int t = q.poll();
            if (t > 0 && dp[t-1] == 88888888) {
                dp[t-1] = Math.min(dp[t-1],dp[t] + 1);
                q.add(t-1);
            }
            if (t < n-1 && dp[t+1] == 88888888) {
                dp[t+1] = Math.min(dp[t+1],dp[t] + 1);
                q.add(t+1);
            }
            if (map.containsKey(arr[t])) {
                for (int num : map.get(arr[t])) {
                    if (num != t && dp[num] == 88888888) {
                        dp[num] = Math.min(dp[num],dp[t] + 1);
                        q.add(num);
                        if ( num == n - 1) {
                            return dp[num];
                        }
                    }
                }
                map.remove(arr[t]);
            }
        }
        return dp[n-1];
    }

}
